# -*- coding: utf-8 -*-

# File Name： board_label
# Description :
# Author : lirui
# create_date： 2022/9/15
# Change Activity:
from ml import is_str


class BoardLabel:
    def __init__(self,
                 cable_num,
                 cable_type,
                 local_addr,
                 local_u,
                 local_board_num,
                 opposite_addr,
                 opposite_u,
                 opposite_board_num,
                 local_port_num=None,
                 opposite_port_num=None):
        """

        Args:
            cable_num:
            cable_type:
            local_addr:
            local_u:
            local_board_num:
            opposite_addr:
            opposite_u:
            opposite_board_num:
            local_port_num:
            opposite_port_num:
        """
        self.cable_num = cable_num
        self.cable_type = cable_type
        # local end
        self.local_addr = local_addr
        self.local_u = local_u
        self.local_board_num = local_board_num
        # opposite end
        self.opposite_addr = opposite_addr
        self.opposite_u = opposite_u
        self.opposite_board_num = opposite_board_num
        # port
        self.local_port_num = local_port_num  # local port num
        self.opposite_port_num = opposite_port_num  # opposite port num
        self._format_values()
        self.label_text = self.generate_label()
        pass

    def generate_label(self):
        """

        Returns:

        """
        if len(self.local_port_num) == 0:
            label_text = f"""本端：{self.local_addr}.{self.local_u}.{self.local_board_num}\n对端：{self.opposite_addr}.{self.opposite_u}.{self.opposite_board_num}"""
        else:
            label_text = f"""本端：{self.local_addr}.{self.local_u}.{self.local_board_num}.{self.local_port_num}\n对端：{self.opposite_addr}.{self.opposite_u}.{self.opposite_board_num}.{self.opposite_port_num}"""
        return label_text

    def _format_values(self):
        """
        format parameters values.
        Returns:

        """
        parameter_dict = self.__dict__
        parameter_keys = parameter_dict.keys()
        for k in parameter_keys:
            value = getattr(self, k)
            if is_str(value):
                value = value.replace(' ', '')
                if len(value) == 0:
                    setattr(self, k, value)
                else:
                    setattr(self, k, value)
        # if self.local_u is not None:
        #     local_u = self.local_u.replace('U', '')
        #     self.local_u = int(local_u)
        # if self.opposite_u is not None:
        #     self.opposite_u = self.opposite_u.replace('U', '')
        #     self.opposite_u = int(self.opposite_u)
